var curPos = 1;
var backPounder = [0,0,0,0];

document.addEventListener("touchstart", function ( event ) {
	console.debug("touchstart");
	console.debug(event);
}, false);

document.addEventListener("touchend", function ( event ) {
	console.debug("touchend");
	console.debug(event);
}, false);

document.addEventListener("touchmove", function ( event ) {
	console.debug("touchmove");
	console.debug(event);
}, false);

document.addEventListener("keydown", function ( event ) {
	if ( event.keyCode === 9 || ( event.keyCode >= 32 && event.keyCode <= 34 ) || (event.keyCode >= 37 && event.keyCode <= 40) ) {
		event.preventDefault();
	}
}, false);

document.addEventListener("keyup", function ( event ) {
	if ( event.keyCode === 9 || ( event.keyCode >= 32 && event.keyCode <= 34 ) || (event.keyCode >= 37 && event.keyCode <= 40) ) {
		switch( event.keyCode ) {
			case 37: // left
			case 8:  // backspace
					if(curPos==3)
					{
						curPos = 1
						//$("#frameFace1")[0].contentWindow.changeGravity(0,0);
						changeGravity(0,0);
						setWalls();
						$('body').css('-webkit-transform', 'translate3d(0px,0px,0px)');
					}
					else
					{
						curPos = 2;
						//$("#frameFace1")[0].contentWindow.changeGravity(-10,0);
						changeGravity(-10,0);
						if(users[POSLEFT]!=null)
						{
							world.DestroyBody(walls[2]);
							walls[2] = null;
						}
						$('body').css('-webkit-transform', 'translate3d(100px,0px,0px)');
					}
					break;
			case 39: // right
			case 32: // space
			case 9:  // tab
					if(curPos==2)
					{
						curPos = 1
						//$("#frameFace1")[0].contentWindow.changeGravity(0,0);
						changeGravity(0,0);
						$('body').css('-webkit-transform', 'translate3d(0px,0px,0px)');
					}
					else
					{
						curPos = 3;
						//$("#frameFace1")[0].contentWindow.changeGravity(10,0);
						changeGravity(10,0);
						if(users[POSRIGHT]!=null)
						{
							world.DestroyBody(walls[3]);
							walls[3] = null;
						}
						$('body').css('-webkit-transform', 'translate3d(-100px,0px,0px)');
					}
					break;
			case 38: // up
			case 33: // pg up
					if(curPos==5)
					{
						curPos = 1
						//$("#frameFace1")[0].contentWindow.changeGravity(0,0);
						changeGravity(0,0);
						setWalls();
						$('body').css('-webkit-transform', 'translate3d(0px,0px,0px)');
					}
					else
					{
						curPos = 4;
						//$("#frameFace1")[0].contentWindow.changeGravity(0,-10);
						changeGravity(0,-10);
						if(users[POSTOP]!=null)
						{
							world.DestroyBody(walls[0]);
							walls[0] = null;
						}
						$('body').css('-webkit-transform', 'translate3d(0px,100px,0px)');
					}
					break;
			case 34: // pg down
			case 40: // down
					if(curPos==4)
					{
						curPos = 1
						//$("#frameFace1")[0].contentWindow.changeGravity(0,0);
						changeGravity(0,0);
						setWalls();
						$('body').css('-webkit-transform', 'translate3d(0px,0px,0px)');
					}
					else
					{
						curPos = 5;
						//$("#frameFace1")[0].contentWindow.changeGravity(0,10);
						changeGravity(0,10);
						if(users[POSBOTTOM]!=null)
						{
							world.DestroyBody(walls[1]);
							walls[1] = null;
						}
						$('body').css('-webkit-transform', 'translate3d(0px,-100px,0px)');
					}
					 break;
		}
		$("#fig"+curPos).focus();
		//location = "#step-"+curPos;
		event.preventDefault();
	}
}, false);

function onWindowDeviceOrientation( event ) 
{
	if(onAccelerometerEnabled)
	{
		if ( event.beta ) 
		{
			var gravityX = -Math.sin( event.gamma * Math.PI / 180 );
			var gravityY = -Math.sin( event.beta * Math.PI / 180 );
			rotateTable(gravityX,gravityY);
		}
	}
}

function activateButton(action,chk)
{
	switch(action)
	{
		case 'A':
					onAccelerometerEnabled = chk.checked;
					break;
		case 'F':
					if(chk.checked)
					{
						worker.postMessage({"method":"connect","url":serverForce,"user":""});
					}
					else
					{
						worker.postMessage({"method":"disconnect","user":""});
					}
					break;
	}
}

var POSLEFT = 0;
var POSRIGHT = 1;
var POSBOTTOM = 2;
var POSTOP = 3;

function translateElement(elementTrans, position)
{
	$(elementTrans).removeClass('box2d');
	elementTrans.style.top = '0px';
	elementTrans.style.left = '0px';
	elementTrans.style.display = 'none';
	backPounder[position]++;
	switch(position)
	{
		case TOP:
					if(users[POSTOP]!=null)
					{
						dropped(elementTrans,users[POSTOP],false);
						$('#leftBox').append(elementTrans);
					}
					$('#topBox').append(elementTrans);
					break;
		case LEFT:
					if(users[POSLEFT]!=null)
					{
						dropped(elementTrans,users[POSLEFT],false);
						$('#leftBox').append(elementTrans);
					}
					break;
		case BOTTOM:
					if(users[POSBOTTOM]!=null)
					{
						dropped(elementTrans,users[POSBOTTOM],false);
						$('#bottomBox').append(elementTrans);
					}
					break;
		case RIGHT:
					if(users[POSRIGHT]!=null)
					{
						dropped(elementTrans,users[POSRIGHT],false);
						$('#rightBox').append(elementTrans);
					}
					break;
	}
	elementTrans.style.position = 'relative';
	$(elementTrans).show('slow');
	drawPounder();
}

var angBack = 10;

function drawPounder()
{
	var incX = backPounder[TOP]-backPounder[BOTTOM];
	var incY = backPounder[RIGHT]-backPounder[LEFT];
	var angleString = "rotateX("+(incX*angBack)+"deg) rotateY("+(incY*angBack)+"deg) translateZ(-200px)";
	document.getElementById("backTable").style.webkitTransform = angleString;
}


var worker = new Worker('./resources/js/driverSocket.js');
worker.addEventListener('message', onDriverResponse, false);
worker.addEventListener('error', onDriverError, false);